package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.jpa.model.Notice;
import com.mxpio.erp.common.order.policy.NoticeContext;
import com.mxpio.erp.common.order.service.NoticeService;
import com.mxpio.erp.common.quality.entity.SoInQualityOrder;
import com.mxpio.erp.common.quality.entity.SoInQualityOrderLine;
import com.mxpio.erp.common.quality.service.SoInQualityOrderLineService;
import com.mxpio.erp.common.quality.service.SoInQualityOrderService;
import com.mxpio.erp.common.vo.OrderExcuteVo;
import com.mxpio.erp.quality.policy.DefaultInQualityOrderNoticePolicy;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "SoInQualityOrderController", description = "销售退货-入库验收单接口")
@RestController
@RequestMapping("/erp/quality/soqo/")
public class SoInQualityOrderController {
	
	@Autowired
	private SoInQualityOrderService inQualityOrderSerivce;
	
	@Autowired
	private SoInQualityOrderLineService inQualityOrderLineSerivce;
	
	@Autowired
	private NoticeService noticeService;
	

	@GetMapping("list")
	@Operation(summary = "销售退货-入库验收单列表", description = "获取销售退货-入库验收单列表", method = "GET")
	public Result<List<SoInQualityOrder>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<SoInQualityOrder> tos = inQualityOrderSerivce.list(SoInQualityOrder.class, criteria);
		return Result.OK(tos);
	}
	
	@GetMapping("page")
	@Operation(summary = "销售退货-入库验收单列表(分页)", description = "获取销售退货-入库验收单列表(分页)", method = "GET")
	public Result<Page<SoInQualityOrder>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<SoInQualityOrder> page = inQualityOrderSerivce.listPage(SoInQualityOrder.class, pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("list/{noticeNo}")
	@Operation(summary = "根据noticeNo获取销售退货-入库验收单", description = "根据noticeNo获取销售退货-入库验收单", method = "GET")
	public Result<SoInQualityOrder> getById(@PathVariable(name = "noticeNo", required = true) String noticeNo) {
		SoInQualityOrder inQualityOrder = inQualityOrderSerivce.getById(SoInQualityOrder.class, noticeNo);
		return Result.OK(inQualityOrder);
	}
	
	/*@RequestMapping("save")
    @Operation(summary = "保存销售退货-入库验收单", description = "保存销售退货-入库验收单")
    public Result<SoInQualityOrder> save(@RequestBody SoInQualityOrder inQualityOrder) {
		inQualityOrderSerivce.save(inQualityOrder,new SmartCrudPolicyAdapter(){
        	@Override
        	public boolean beforeInsert(CrudContext context) {
        		Object o = context.getEntity();
        		if(o instanceof SoInQualityOrderLine){
        			SoInQualityOrder inQualityOrder = context.getParent();
                    ((SoInQualityOrderLine) o).setNoticeNo(inQualityOrder.getNoticeNo());
        		}
        		return true;
        	}
        });
        return Result.OK(inQualityOrder);
    }
	
	@DeleteMapping("remove/{noticeNos}")
	@Operation(summary = "删除销售退货-入库验收单", description = "删除销售退货-入库验收单", method = "DELETE")
	public Result<SoInQualityOrder> remove(@PathVariable(name = "noticeNos", required = true) String noticeNos) {
		String noticeNo[] = noticeNos.split(",");
		for(String key : noticeNo){
			Criteria c = Criteria.create().addCriterion("noticeNo", Operator.EQ, key);
			inQualityOrderLineSerivce.removeBatch(SoInQualityOrderLine.class, c);
			inQualityOrderSerivce.delete(SoInQualityOrder.class, key);
		}
		return Result.OK();
	}*/
	
    @PutMapping("submit/{noticeNo}")
    @Operation(summary = "提交销售退货-入库验收单", description = "提交销售退货-入库验收单", method = "PUT")
    public Result<Object> submit(@PathVariable(name = "noticeNo", required = true) String noticeNo, 
    		@RequestBody OrderExcuteVo orderExcuteVo) {
    	SoInQualityOrder notice = inQualityOrderSerivce.getById(SoInQualityOrder.class, noticeNo);
		if (notice.getNoticeStatus().equals(NoticeStatusEnums.NoticeStatus.CONFIRM.getStatusCode())){
			return Result.error("单据状态已发生变化，已确认状态单据不允许重复确认！");
		}
    	NoticeContext context = noticeService.execute(notice, new DefaultInQualityOrderNoticePolicy(orderExcuteVo.getLines()));
    	if(CollectionUtils.isEmpty(context.getErrors())) {
    		return Result.OK();
    	}else {
    		return Result.error(context.getErrors().get(0));
    	}
    }

	@GetMapping("listbynos/{noticeNos}")
	@Operation(summary = "根据noticeNos获取对应销售退货-入库验收单及明细", description = "根据noticeNos获取销售退货-入库验收单及明细", method = "GET")
	public Result<List<SoInQualityOrder>> getByIds(@PathVariable(name = "noticeNos", required = true) String noticeNos) {
		return inQualityOrderSerivce.listByIds(noticeNos);
	}

	@PutMapping("reject/{noticeNo}")
	@Operation(summary = "拒绝销售退货-入库验收单", description = "拒绝销售退货-入库验收单", method = "PUT")
	public Result<? extends Notice> close(@Parameter(description="销售退货-入库验收单单号") @PathVariable(name = "noticeNo", required = true) String noticeNo) {
		SoInQualityOrder notice = inQualityOrderSerivce.getById(SoInQualityOrder.class,noticeNo);
		return noticeService.updateNoticeStatus(notice, NoticeStatusEnums.NoticeStatus.REJECT);
	}
}
